package mylistdemo001;

import java.util.ArrayList;
import java.util.Scanner;

/**添加用户对象并判断是否存在
 * 需求：
 * 1.main方法中定义一个集合,存入三个集合对象
 * 用户属性为：id,username,password
 * 2.要求：定义一个方法，根据id查找对应的用户信息。
 * 如果存在，返回true
 * 如果不存在，返回false
 */
public class ArrayListPractice05 {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();

        User u = new User(001,"张三","2016lcm");
        User u1 = new User(002,"李四","20216lcm");
        User u2 = new User(003,"王二","20156lcm");
        list.add(u);
        list.add(u1);
        list.add(u2);

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要查找的id");
        int id = sc.nextInt();
        boolean flag = lookUp(list,id);
        System.out.println(flag);
    }

    public static boolean lookUp(ArrayList<User> list,int id){

        for (int i = 0; i < list.size(); i++){
            User a = list.get(i);
            if (id == a.getId()){
             return true;
            }
        }
        return false;
    }

}
